<?php
/**
 * | @author 李鹏博^life <907215837@qq.com>
 * +----------------------------------------------------------------------
 * | @copyright 2016 - 2019 HQ
 * +----------------------------------------------------------------------
 * | @version $Id: 2019/5/2 16:31 jiahe_new memberServer.php 李鹏博^life $
 * +----------------------------------------------------------------------
 */

namespace app\api\server\member;

use app\common\common\lib\OpensslEncrypt;
use app\common\common\model\member\member;
use app\common\common\model\oldModel\order\order;
use app\common\common\model\oldModel\order\OrderItem;
use app\common\common\model\oldModel\member\member as oldMember;
use app\common\common\model\oldModel\member\memberCard;
use app\common\common\model\oldModel\member\memberCardLog;
use app\common\common\Server\verifiableServer;
use app\api\server\storeServer;
use app\common\common\Server\commonServer;
use \think\Controller;
use app\common\common\model\oldModel\store\Store;
use app\common\common\model\oldModel\goods\Goods;
use app\common\common\model\oldModel\goods\GoodsItem;
use app\common\common\model\oldModel\goods\StoreGoods;
use think\Db;
use app\common\common\model\oldModel\coupons\Coupons;
use app\common\common\model\oldModel\coupons\CouponsTpl;
use app\common\common\model\order\banner;
use think\Env;


class  myOrderServer {

    public $status = true;
    public $error = '';
    public $order = '';

    /**
     * 订单列表。
     * @param int $sta 0为待评价 1为全部
     * @param int $member_id 用户唯一标识
     * @return array
     */
    public function orderList($sta=1,$limit='',$member_id = 0){

        //获取订单；
        $this->order = new order();
        $order_map['member_id'] = $member_id;//用户标示

        if($sta == 0){
            $order_map['status_order'] = array('in','1,2,3,4'); //是否评价 如果status_order大于0小于5，则为待评价 如果 status_order为12则为已评价
        }
        $length = 10;

        $all_order = $this->order->selectByWhere($order_map,$limit,'create_time desc');

        $arr_column_store_id = array_column($all_order['data'], 'store_id');//获取所有门店id
        $arr_column_order_id = array_column($all_order['data'], 'order_id');//获取所有订单id

        //获取所有店面信息
        $new_Store = new Store();
        $store_map['store_id'] = array('in',implode(',',$arr_column_store_id));
        $all_store = $new_Store->allByWhere($store_map);
        $all_column_store_null = array_column($all_store,null,'store_id');

        //获取所有订单详情信息
        $OrderItem = new OrderItem();
        $OrderDetail_map['order_id'] = array('in',implode(',',$arr_column_order_id));
        $all_OrderItem = $OrderItem->allByWhere($OrderDetail_map);

        //判断订单详情中store_goods_id是否为空，如果为空则查询旧的，相反则根据store_goods_id查询
        $arr_no_store_goods_id=[];
        $arr_yes_store_goods_id=[];
        foreach($all_OrderItem as $key => $value){
            if(empty($value['store_goods_id'])){
                $arr_no_store_goods_id[] = $value['goods_id'];
            }else{
                $arr_yes_store_goods_id[] = $value['store_goods_id'];
            }
        }

        //查询store_goods_id字段不为空的数据
        $Goods = new Goods();
        $all_yes_goods = [];
        $all_no_goods = [];
        if(!empty($arr_yes_store_goods_id)){

            $Goods_yes_map['sg.id']  = array('in',$arr_yes_store_goods_id);
            $all_yes_goods=$Goods->findStoreGoods($Goods_yes_map);
        }

        //查询store_goods_id为空的数据
        if(!empty($arr_no_store_goods_id)){

            $Goods_no_map['sg.dkid']  = array('in',$arr_no_store_goods_id);
            $all_no_goods=$Goods->findStoreGoods($Goods_no_map);

        }

        $all_goods = array_merge($all_yes_goods,$all_no_goods);

        //获取所有商品信息
        foreach($all_goods as $key => $value){

            $all_goods[$key]['good_pic'] = config('aliPicUrl').$value['did'].'.png';
        }

        $all_column_goods_id_null = array_column($all_goods,null,'dkid');

        /**
         * 将订单详情格式化，查找商品信息
         * $arr_two 第一个键为订单id 第二个键为商品id
         */
        $arr_two=[];
        foreach($all_OrderItem as $key => $value){
            $arr[$value['order_id']][] = $value;
            $all_column_order_id = array_column($arr[$value['order_id']],'goods_id','goods_id');
            $arr_two[$value['order_id']] = $all_column_order_id;

        }

        //返回所需数据；
        $arr_data=[];

        foreach ($all_order['data'] as $key => $value){
            $arr_data[$key]['order_code'] = $value['order_code'];
            $arr_data[$key]['order_id'] = $value['order_id'];
            $arr_data[$key]['created_at'] = date('m/d H:i',strtotime($value['create_time'])); //下单时间
            $arr_data[$key]['money_goods'] = $value['money_pay']; //应付价格
            $arr_data[$key]['status_order'] = $value['status_order'];
            $arr_data[$key]['status_pay'] = $value['status_pay'];
            $arr_data[$key]['status_order_info'] = config('pay.status_order')[$value['status_order']];
            $arr_data[$key]['store_name'] = isset($all_column_store_null[$value['store_id']]['sname']) ? $all_column_store_null[$value['store_id']]['sname']:''; //店面
            $res = [];
            if(empty($arr_two[$value['order_id']])){

                continue;
            }
            $i=0;
            foreach($arr_two[$value['order_id']] as $k => $val){
                if($i<3){
                    if(empty($all_column_goods_id_null[$k])){
                        $res[$k]['good_pic'] =config('goods_cover_url');
                        $i++;
                        continue;
                    }
                    $res[$k] = $all_column_goods_id_null[$k];
                }
                $i++;

            }
            $arr_data[$key]['good_info'] = $res;
        }
        $res_data['data']=$arr_data;
        $res_data['count']=$all_order['count'];
        return $res_data;
    }

    /**
     * 订单和支付状态
     * @param $status_order 订单状态
     * @param $status_pay 付款状态
     */
    public function orderStatusInfo($status_order,$status_pay){

        $arr_info = [];

        if(($status_order == STATUS_NEW_ORDER || $status_order == 0) && $status_pay == STATUS_PAYMENT_PAY) {

            $arr_info['status_order_info'] = '创建订单';
        }elseif(($status_order == STATUS_NEW_ORDER || $status_order == STATUS_SETTlLED_ORDER) && $status_pay == STATUS_SUCCESS_PAY){

            $arr_info['status_order_info'] = '待评价';
        }elseif($status_order == STATUS_UNSUBSCRIBED_ORDER && $status_pay == STATUS_REFUND_ORDER){

            $arr_info['status_order_info'] = '已退单';
        }elseif($status_order == STATUS_COMPLETED_ORDER && $status_pay == STATUS_SUCCESS_PAY){

            $arr_info['status_order_info'] = '已完成';
        }elseif($status_order == STATUS_MANUALLY_CANCEL_ORDER || $status_order == STATUS_OVERTIME_ORDER){

            $arr_info['status_order_info'] = '已取消';
        }else{

            $arr_info['status_order_info'] = '异常订单';
        }
        return $arr_info;
    }


    /**
     * 判断订单是否评价
     * @param $order_id
     * @return bool
     * @author 李鹏博^life <907215837@qq.com>
     */

    public  function  isOrderEvaluate($order_id){

        $this->order = new order();
        $where['order_id'] = $order_id;
        $find_order = $this->order->findByWhere($where);

        if($find_order['status_order'] != 12){

            return 1;
        }
    }
    /**
     * 查询订单评价商品
     * @param $order_id
     * @return array
     */
    public function orderGoods($order_id){


        //获取订单想详情信息
        $OrderDetail = new OrderItem();
        $OrderDetail_map['order_id'] = $order_id;
        $all_OrderDetail = $OrderDetail->allByWhere($OrderDetail_map);
        $arr_yes_store_goods_id = [];
        $arr_no_store_goods_id = [];
        foreach($all_OrderDetail as $key => $value){
            if(empty($value['store_goods_id'])){
                $arr_no_store_goods_id[] = $value['goods_id'];
            }else{
                $arr_yes_store_goods_id[] = $value['store_goods_id'];
            }
        }

        //查询store_goods_id字段不为空的数据
        $Goods = new Goods();
        $all_yes_goods = [];
        $all_no_goods = [];
        if(!empty($arr_yes_store_goods_id)){

            $Goods_yes_map['sg.id']  = array('in',$arr_yes_store_goods_id);
            $all_yes_goods=$Goods->findStoreGoods($Goods_yes_map);
        }
        //查询store_goods_id为空的数据
        if(!empty($arr_no_store_goods_id)){

            $Goods_no_map['sg.dkid']  = array('in',$arr_no_store_goods_id);
            $all_no_goods=$Goods->findStoreGoods($Goods_no_map);

        }

        $all_goods = array_merge($all_yes_goods,$all_no_goods);


        //将此订单所有商品id放入数组中,查找所有的商品信息，根据key查找对应的商品信息
        $all_goods_column = array_column($all_goods,null,'dkid');

        //查找和组装商品信息
        $arr=[];
        foreach($all_OrderDetail as $key => $value){

            $arr[$key]['item_id'] = $value['item_id'];
            $arr[$key]['order_id'] = $value['order_id'];
            $arr[$key]['goods_id'] = $value['goods_id'];
            $arr[$key]['sta'] = 0;
            $arr[$key]['did'] = isset($all_goods_column[$value['goods_id']]['did'])?$all_goods_column[$value['goods_id']]['did']:0;
            $arr[$key]['goods_name'] = isset($all_goods_column[$value['goods_id']]['name'])?$all_goods_column[$value['goods_id']]['name']:'';
        }

        return $arr;
    }

    /**
     * 订单详情
     * @param $order_id 订单id
     * @return mixed
     */
    public function orderItem($order_id = 0){
        
        //获取订单；

        $this->order = new order();
        $order_map['order_id'] = $order_id;//新后台标示 1为旧平台 2为新后台
        $find_order = $this->order->findByWhere($order_map);

        $new_find_order['order_id'] = $order_id;
        $new_find_order['money_tableware'] = $find_order['money_tableware']; //餐具费
        $new_find_order['money_packing'] = $find_order['money_packing'];
        $new_find_order['money_goods_discount'] = $find_order['money_goods_discount'];//会员优惠价格
        $new_find_order['money_goods'] = $find_order['money_goods'];//商品售价总和
        $new_find_order['money_pay'] = $find_order['money_pay'];//应付价格：money_delivery+money_packing+money_goods-money_economize

        $new_find_order['order_code'] = $find_order['order_code']; //订单编号
        $new_find_order['table_num'] = $find_order['table_num']; //桌号
        $new_find_order['people'] = $find_order['people']; //就餐人数
        $new_find_order['create_time'] = date('Y/m/d H:i:s',strtotime($find_order['create_time'])); //创建时间
        $new_find_order['created_at'] = date('m/d H:i',strtotime($find_order['create_time'])); //下单时间
        $new_find_order['pay_type'] = config('pay.order_pay_type')[$find_order['pay_type']]; //支付方式
        $new_find_order['status_pay'] = $find_order['status_pay']; //支付状态

        $new_Store = new Store();
        $store_map['store_id'] = $find_order['store_id'];
        $all_store = $new_Store->findByWhere($store_map);
        $new_find_order['store_name'] = $all_store['sname'];
        $new_find_order['old_money'] = $find_order['money_tableware']+$find_order['money_half_two']+$find_order['money_goods']+$find_order['money_goods_discount']; //原价
        $new_find_order['status_order'] = $find_order['status_order'];
        $new_find_order['status_pay'] = $find_order['status_pay'];
        $new_find_order['status_info'] = $this->orderStatusInfo($find_order['status_order'],$find_order['status_pay'])['status_order_info'];; //订单状态信息

        //获取订单想详情信息
        $OrderItem = new OrderItem();
        $OrderDetail_map['order_id'] = $order_id;
        $order_detail_field[] = 'goods_name';
        $order_detail_field[] = 'amount';
        $order_detail_field[] = 'old_price';
        $order_detail_field[] = 'current_price';
        $all_OrderDetail = $OrderItem->allByWhere($OrderDetail_map,$order_detail_field);
        $new_find_order['order_goods'] = $all_OrderDetail;
        $new_find_order['order_goods_count'] = count($all_OrderDetail);
        $new_find_order['money_reduct'] = $find_order['money_reduct']; //满减

        $new_find_order['money_half_two'] = $find_order['money_half_two']; //第二份半价
        //判断早餐卡和共享卡
        $new_find_order['money_coupon'] = 0;
        $new_find_order['breakfast_sharing'] = 0;
        $coupon=0;
        $new_find_order['coupon_name'] = '';
        if($find_order['pay_type'] == 25 || $find_order['pay_type'] == 30){
            $new_find_order['breakfast_sharing'] = $find_order['money_coupon'];

        }else if(!empty($find_order['coupon_id'])){
            //判断优惠券抵扣
            $new_find_order['money_coupon'] = $find_order['money_coupon'];

            //查找优惠卷名称
            $couponsModel = new Coupons();
            $coupon_map['coupon_id'] = $find_order['coupon_id'];
            $find_coupons = $couponsModel->findToArray($coupon_map);
            $CouponsTpl = new CouponsTpl();
            $CouponsTpl_map['coupon_id'] = $find_coupons['tpl_id'];
            $find_CouponsTpl = $CouponsTpl->findToArray($CouponsTpl_map);

            $new_find_order['coupon_name'] = $find_CouponsTpl['name'];
        }

        //节省价格
        $new_find_order['money_economize'] = sprintf("%.2f",$find_order['money_reduct']+$find_order['money_half_two']+$new_find_order['breakfast_sharing']+$new_find_order['money_coupon']+$new_find_order['money_goods_discount']);

        //订单成功页面banner
        $banner = new banner();
        $banner_map['identity'] = 0;
        $all_banner = $banner->allByWhere($banner_map);
        foreach($all_banner as $key => $value){
            $all_banner[$key]['smallimage']=$_SERVER["REQUEST_SCHEME"].'://'.$_SERVER['SERVER_NAME'].$value['smallimage'];
        }

        $new_find_order['banner']= $all_banner; //订单支付成功页面轮播

        return $new_find_order;
    }


    /**
     * 取消订单
     * @param $order_id 订单id
     * @param $member_id 用户标示
     *
     */
    public function cancelOrder($order_id,$member_id){

        $this->order = new order();
        $order_map['order_id'] = $order_id;
        $order_data['status_order'] = 11;//用户取消支付
        $order_data['status_pay'] = 10;//用户取消支付
        $res_find = $this->order->findByWhere($order_map);
        if(!empty($res_find['coupon_id'])){
            $couponsModel = new Coupons();
            $couponsModel->where( array('coupon_id' => $res_find['coupon_id'] ) )->update( ['status_coupon' => 2]);
        }
        $res = $this->order->updateByOpenId($order_map,$order_data);
        return  $res;



    }







}